package com.xq.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xq.shop.config.Redis;
import com.xq.shop.dao.domain.entity.Notice;
import com.xq.shop.dao.mapper.NoticeMapper;
import com.xq.shop.dto.EditNoticeDto;
import com.xq.shop.dto.IntegerDto;
import com.xq.shop.dto.QueryDto;
import com.xq.shop.service.INoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.shop.vo.NoticeVo;
import enums.DataStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.BasePage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-22
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Resource
    private Redis redis;

    @Override
    public BasePage<NoticeVo> pageQuery(QueryDto dto) {
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notice::getStatus, DataStatus.NORMAL.getCode());
        if (Objects.nonNull(dto.getContent())) {
            wrapper.like(Notice::getTitle, dto.getContent());
        }
        wrapper.orderByDesc(Notice::getId);
        IPage<Notice> page = page(new Page<>(dto.getPage(), dto.getLimit()), wrapper);
        List<Notice> noticeList = page.getRecords();
        if (noticeList.isEmpty()) {
            return new BasePage<NoticeVo>().initPage(dto.getPage(), dto.getLimit(), page.getTotal(), page.getPages(), new ArrayList<>());
        }
        List<NoticeVo> voList = beanToVo(noticeList);
        return new BasePage<NoticeVo>().initPage(dto.getPage(), dto.getLimit(), page.getTotal(), page.getPages(), voList);
    }

    private List<NoticeVo> beanToVo(List<Notice> noticeList) {

        List<NoticeVo> voList = new ArrayList<>();
        noticeList.forEach(notice -> {
            NoticeVo vo = new NoticeVo();
            BeanUtils.copyProperties(notice, vo);
            voList.add(vo);
        });
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editNotice(EditNoticeDto dto, Integer userId) {
        Notice notice = new Notice();
        if (dto.getId().equals(0)) {
            notice.setCreateUser(userId);
            BeanUtils.copyProperties(dto, notice);
            save(notice);
        } else {
            notice = getById(dto.getId());
            BeanUtils.copyProperties(dto, notice);
            notice.setUpdateUser(userId);
            updateById(notice);
        }
        redis.remove("notice");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeNotice(IntegerDto dto, Integer id) {
        Notice notice = getById(dto.getValue());
        if (Objects.nonNull(notice)) {
            notice.setStatus(DataStatus.INVALID.getCode());
            notice.setUpdateUser(id);
            updateById(notice);
        }
        redis.remove("notice");
    }

    @Override
    public List<NoticeVo> queryNotice(QueryDto dto) {
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Notice::getStatus, DataStatus.NORMAL.getCode());
        if (Objects.nonNull(dto.getContent())) {
            wrapper.like(Notice::getTitle, dto.getContent());
        }
        wrapper.orderByDesc(Notice::getId);
        List<Notice> noticeList = list(wrapper);
        if (noticeList.isEmpty()) {
            return new ArrayList<>();
        }
        return beanToVo(noticeList);
    }
}
